
const fs = require('fs/promises');
const path = require('path');
const del = require('del');
const { Listr } = require('listr2');
const utils = require('./lib/utils');
const maven = require('./lib/processor/maven');
const copyDir = require('recursive-copy');
const simpleGit = require('simple-git/promise');

const work = async () => {

    let project = 'jay-web-demo';
    // let project = 'my-web-demo';
    let pdir = path.resolve(__dirname, project);
    let tmpRoot = path.join(pdir, ".tmp");
    
    await del([ tmpRoot ]);

    await utils.lookUpDir(pdir, async (filename, filePath) => {
        await del([ filePath ]);
    });

    let tmpfolder = path.join(tmpRoot, "template_" + Math.round(100000 * Math.random()) + "_" + Date.now());
    await fs.mkdirp(tmpfolder);

    await simpleGit().clone('https://gitee.com/lgks701/ugeez-starterkit-template-java-spring-web-project.git', tmpfolder);
 
    await del([ path.resolve(tmpfolder, '.git') ]);

    await copyDir(tmpfolder, pdir);

    await maven.process(pdir, project, {}, { 'ns': 'com.ugeez.app' });

    await del([ tmpRoot ]);
}

const tax = () => {
    const ms = 13000;
    const m = 15;
    const total = ms * m;

    const dec = [
        1000,  //55岁以下
        4000,  //16岁以下子女
        '15%' //妈妈工作
    ];
    
    const levels = [
        [ 0, 20000, 0 ],
        [ 20000, 30000, 2 ],
        [ 30000, 40000, 3.5 ],
        [ 40000, 80000, 7.0 ],
        [ 80000, 120000, 11.5 ],
        [ 120000, 160000, 15.0 ],
        [ 160000, 200000, 18.0 ],
        [ 200000, 240000, 19.0 ],
        [ 240000, 280000, 19.5 ],
        [ 280000, 320000, 20.0 ],
        [ 320000, Number.MAX_VALUE, 22.0 ]
    ];

    let tmpIncome = 0;
    let tmpDec = 0;
    let tax = 0;

    //dec
    for (let d of dec) {
        if (String(d).indexOf('%') > 0) {
            tmpDec += total * Number(String(d).replace('%', '')) / 100;
        } else {
            tmpDec += d;
        }
    }

    tmpIncome = total - tmpDec;
    console.log('total: ', total);
    console.log('tmpDec: ', tmpDec);
    console.log('total - tmpDec: ', tmpIncome);

    //tax
    for (let i = levels.length - 1; i >= 0; i --) {
        let lv = levels[i];
        if (tmpIncome >= lv[0] && tmpIncome <= lv[1]) {
            let lvTax = (tmpIncome - lv[0]) * (lv[2] / 100);
            tax += lvTax;
            tmpIncome = lv[0];
            // console.log(lv[0], lv[2], lvTax);
        }
    }

    console.log('tax: ', tax);
}

// work();
// tax();

function test() {
    require('./lib/processor/code-class').javaClassProcess({
        pdir: process.cwd(),
    }, {});
}

test();